package com.turing.data.scripts.contract.filter


import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO

class OperatorTele_30211_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        String REGEX_1 = /.*资产托管人.*/
        String REGEX_2 = /.*托管人概况.*/
        String REGEX_3 = "管理人"
        String REGEX_4 = /.*托管人.*/

        def textPathMap = dependParams.textPathMap

        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
//                    return  resultVOs
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                def start = resultVOs.get(i).start
                def end = resultVOs.get(i).end
                while( expandOffset < 150 && start > expandOffset  ){
                    def expandStr = rawText.substring( start - expandOffset,start )

                    if( (expandStr =~ REGEX_1 || expandStr =~ REGEX_2) && !expandStr.contains(REGEX_3) ){
                        printf "\n"
                        printf expandStr
                        printf "\n"
                        trueValueSet.add( resultVOs.get(i) )
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                def start = resultVOs.get(i).start
                def end = resultVOs.get(i).end
                while( expandOffset < 150 && start > expandOffset  ){
                    def expandStr = rawText.substring( start - expandOffset,start )
                    if( expandStr =~ REGEX_4 && !expandStr.contains(REGEX_3) ){
                        trueValueSet.add( resultVOs.get(i) )
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }
        return  resultVOs
    }

}
